//
//  StoreMenuView.m
//  ebook
//
//  Created by 吴超飞 on 16/3/8.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "StoreMenuView.h"
#import "EbookNetRequest.h"
#import "UIImage+RTTint.h"

@interface StoreMenuView(){
    
}

@property (nonatomic, retain) NSMutableArray *menuDataArray;
@property (nonatomic, retain) NSMutableArray *menuButtonArray;
@property (nonatomic, retain) UIImageView *switchImageView;

@end

@implementation StoreMenuView

-(instancetype)initWithFrame:(CGRect)frame withMenuArray:(NSArray *)menu{
    
    self = [super initWithFrame:frame];
    
    self.menuDataArray = [NSMutableArray array];
    self.menuButtonArray = [NSMutableArray array];
    self.selectedIndex = -1;
    
    if (self) {
    
        for (NSDictionary *obj in menu) {
            StoreMenu *sm = [[StoreMenu alloc] initWithDictionary:obj];
            [self.menuDataArray addObject:sm];
        }
        
        [self drawView];
    }
    return self;
}

- (void)drawView{
    
    if(self.menuDataArray.count<1)return;
    float viewWidth = MTScreenW/self.menuDataArray.count;
    __weak typeof(self) weakSelf = self;
    [self.menuDataArray enumerateObjectsUsingBlock:^(StoreMenu *sm, NSUInteger idx, BOOL * _Nonnull stop) {
        __strong __typeof(self) strongSelf = weakSelf;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(idx*viewWidth, 0, viewWidth, 43)];
        btn.tag = idx;
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitle:sm.title forState:UIControlStateNormal];
        [btn setTitleColor:MTConfigColor(@"MTStoreMenu_buttonTitleNormal_Color") forState:UIControlStateNormal];
        [btn setTitleColor:MTConfigColor(@"MTStoreMenu_buttonTitleSelected_Color") forState:UIControlStateSelected];
        [btn addTarget:strongSelf action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [strongSelf.menuButtonArray addObject:btn];
        [strongSelf addSubview:btn];
    }];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, MTScreenW,0.5)];
    lineView.backgroundColor = MTConfigColor(@"MTStoreMenu_lineBg_Color");
    [self addSubview:lineView];
    
    self.selectedIndex = 0;
    self.switchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(viewWidth*_selectedIndex, 43, viewWidth, 1)];
    self.switchImageView.image = [[UIImage imageNamed:@"type_line"] rt_tintedImageWithColor:MTConfigColor(@"MTStoreMenu_switchImgView_Color")];
    [self addSubview:self.switchImageView];
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    
    _lastSelectedIndex = _selectedIndex;
    _selectedIndex = selectedIndex;
    [self.menuButtonArray enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL * _Nonnull stop) {
        btn.selected = idx==_selectedIndex?YES:NO;
    }];
}

-(void)setSelectedButtonIndex:(NSInteger)index{
    
    if (self.menuButtonArray.count>index) {
        [self btnClick:[self.menuButtonArray objectAtIndex:index]];
    }
}

//根据序号模拟点击菜单
-(void)performClickBtn:(NSInteger)index{
    UIButton *btn=[UIButton new];
    btn.tag = index;
    [self btnClick:btn];
}

-(void)btnClick:(UIButton *)btn{
    
    [self switchMenuIndex:btn.tag];
    
    if (self.menuDataArray.count < btn.tag) return;
    
    if ([self.delegate respondsToSelector:@selector(storeMenuView:didSelected:)]) {
        [self.delegate storeMenuView:self didSelected:[self.menuDataArray objectAtIndex:btn.tag]];
    }
}

-(void)switchMenuIndex:(NSInteger)index{
    
    self.selectedIndex = index;
    
    if (self.menuDataArray.count < index+1) return;
    
    CGRect btnRect = ((UIButton *)[self.menuButtonArray objectAtIndex:index]).frame;
    CGRect switchRect = btnRect;
    switchRect.origin.y = self.switchImageView.frame.origin.y;
    switchRect.size.height = self.switchImageView.frame.size.height;
    
    [UIView animateWithDuration:0.2 animations:^{
        [self.switchImageView setFrame:switchRect];
    }];
}

-(StoreMenu *)currentMenu{
    return self.menuDataArray.count > self.selectedIndex ? [self.menuDataArray objectAtIndex:self.selectedIndex]:nil;
}

//获取菜单的url
-(NSString *)originalUrlOfMenu:(NSInteger)index{
    return self.menuDataArray.count > index ? ((StoreMenu *)[self.menuDataArray objectAtIndex:index]).url:nil;
}


@end
